from rest_framework import serializers
from .models import Resume, Education, WorkExperience, ResumeAttachment,ResumeTemplate

class EducationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Education
        fields = '__all__'

class WorkExperienceSerializer(serializers.ModelSerializer):
    class Meta:
        model = WorkExperience
        fields = '__all__'


class ResumeAttachmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = ResumeAttachment
        fields = '__all__'

class ResumeSerializer(serializers.ModelSerializer):
    educations = EducationSerializer(many=True, required=False)
    work_experiences = WorkExperienceSerializer(many=True, required=False)
    attachments = ResumeAttachmentSerializer(many=True, required=False)

    class Meta:
        model = Resume
        fields = '__all__'

    def create(self, validated_data):
        educations_data = validated_data.pop('educations', [])
        work_experiences_data = validated_data.pop('work_experiences', [])
        project_experiences_data = validated_data.pop('project_experiences', [])
        skills_data = validated_data.pop('skills', [])
        attachments_data = validated_data.pop('attachments', [])
        resume = Resume.objects.create(**validated_data)
        for edu in educations_data:
            Education.objects.create(resume=resume, **edu)
        for work in work_experiences_data:
            WorkExperience.objects.create(resume=resume, **work)
        for att in attachments_data:
            ResumeAttachment.objects.create(resume=resume, **att)
        return resume

    def update(self, instance, validated_data):
        # 可根据需要实现嵌套更新
        return super().update(instance, validated_data)

class ResumeTemplateSerializer(serializers.ModelSerializer):
    class Meta:
        model = ResumeTemplate
        fields = '__all__'